package mall.dao;

import com.alibaba.druid.util.StringUtils;
import mall.model.Admin;
import mall.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 *
 * @author Yonni
 * @version 1.8.0_201
 * @date 2020-05-27 18:23
 *
 * */
public class AdminDaoImpl implements AdminDao {

    public Admin login(Admin admin) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        Admin query = null;
        try {
            query = queryRunner.query("select * from admin where email = ? and pwd = ?",
                    new BeanHandler<Admin>(Admin.class),
                    admin.getEmail(),
                    admin.getPwd());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }

    public List<Admin> allAdmins() {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        List<Admin> admins = null;
        try {
            admins = queryRunner.query("select * from admin", new BeanListHandler<Admin>(Admin.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return admins;
    }

    public List<Admin> getSearchAdmins(Admin admin) {
        //获取SQL语句和参数
        Map<String, Object> map = getDynamicSql(admin);
        String sql = (String) map.get("sql");
        List<String> params = (List<String>) map.get("params");
        //查询数据库
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        //Object...params表示数组类型
        List<Admin> adminList = null;
        try {
            adminList = queryRunner.query(sql, new BeanListHandler<Admin>(Admin.class), params.toArray());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return adminList;
    }

    public void deleteAdmin(String id) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        try {
            int update = queryRunner.update("delete from admin where id = ?", id);
            //System.out.println(update);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public Admin getAdminsInfo(String id) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        Admin admin = null;
        try {
            admin = queryRunner.query("select * from admin where id = ?", new BeanHandler<Admin>(Admin.class), id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return admin;
    }

    public Admin getSearchAdminByEmail(Admin admin) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        Admin query = null;
        try {
            query = queryRunner.query("select * from admin where email = ?", new BeanHandler<Admin>(Admin.class), admin.getEmail());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }

    public int addAdmin(Admin admin) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        Object[] adminArray = {admin.getEmail(), admin.getPwd(), admin.getNickname()};
        int update = 0;
        try {
            update = queryRunner.update("insert into admin values(null, ?, ?, ?)", adminArray);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return update;
    }

    public void updateAdmin(Admin admin) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        Object[] adminArray = {admin.getEmail(), admin.getPwd(), admin.getNickname(), admin.getId()};
        try {
            int update = queryRunner.update("update admin set email = ?, pwd = ?, nickname = ? where id = ?", adminArray);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    /**
     * 动态SQL
     * 1. 当账户有数据时：select * from admin where email like ?
     * 2. 当昵称有数据时：select * from admin where nickname like ?
     * 3. 当账户和昵称均有数据时：select * from admin where email like ? and nickname like ?
     * */
    private Map<String, Object> getDynamicSql(Admin admin) {
        //此处 1=1 的妙处在于避免下面if语句中where关键字的重复
        String base = "select * from admin where 1 = 1 ";
        List<String> params = new ArrayList<String>();
        if (!StringUtils.isEmpty(admin.getNickname())) {
            base = base + "and nickname like ?";
            params.add("%" + admin.getNickname() + "%");
        }
        if (!StringUtils.isEmpty(admin.getEmail())) {
            base = base + "and email like ?";
            params.add("%" + admin.getEmail() + "%");
        }
        //map中的值有String和List，因此用Object接收
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("sql", base);
        map.put("params", params);
        return map;
    }
}
